//
//  CNFuncButtonsView.m
//  BeeQuick_19
//
//  Created by tanky on 2016/9/23.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNFuncButtonsView.h"
#import "CNFucButtonsData.h"
#import <SDWebImage/UIButton+WebCache.h>
#import "CNButton.h"
#import "CNHomeTableViewController.h"

@interface CNFuncButtonsView ()

@property(nonatomic,strong)NSArray *funcButtonsArray;

@end

@implementation CNFuncButtonsView

#pragma mark View创建的时候就去获取数据
- (instancetype)init{
    [self loadFuncBtnsData];
    return [super init];
}

- (instancetype)initWithFrame:(CGRect)frame{
    [self loadFuncBtnsData];
    return [super initWithFrame:frame];
}

#pragma mark 有数据赋值的时候 就去添加buttons
- (void)setFuncButtonsArray:(NSArray *)funcButtonsArray{
    if (_funcButtonsArray != funcButtonsArray) {
        _funcButtonsArray = funcButtonsArray;
        [self addButtonsView];
    }
}

#pragma mark 加载网络数据 并赋值
- (void)loadFuncBtnsData{
    CNHTTPSessionManager *manager = [CNHTTPSessionManager sharedManager];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    // type = 0/1
    [param setValue:@"1" forKey:@"call"];
    
    [manager request:CNHttpMethodPost componentUrlString:@"focus.json.php" parameters:param completion:^(id response, NSError *error) {
        NSArray *arr = [NSArray yy_modelArrayWithClass:[CNFucButtonsData class] json:response[@"data"][@"icons"]];
        self.funcButtonsArray = arr.copy;
    }];
}

#pragma mark 添加Buttons到屎大颗View上
- (void)addButtonsView{
    self.axis = UILayoutConstraintAxisHorizontal;
    self.alignment = UIStackViewAlignmentFill;
    self.spacing = 0;
    self.distribution = UIStackViewDistributionFillEqually;
    for (CNFucButtonsData *model in self.funcButtonsArray) {
        CNButton *btn = [CNButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:model.name forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [btn sd_setImageWithURL:[NSURL URLWithString:model.img] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"v2_placeholder_square"]];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        btn.contentEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15);
        btn.imageEdgeInsets = UIEdgeInsetsMake(-25, 0, 0, 0);
        btn.titleEdgeInsets = UIEdgeInsetsMake(60, -138, 0, 0);
        btn.backgroundColor = [UIColor whiteColor];
        CNHomeTableViewController *homeVC = [self getCurrentViewController];
        btn.urlString = model.customURL;
        [btn addTarget:homeVC action:@selector(jumpToWebViewWithButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addArrangedSubview:btn];
    }
}

/** 获取当前View的控制器对象 */
- (CNHomeTableViewController *)getCurrentViewController{
    UIResponder *next = [self nextResponder];
    do {
        if ([next isKindOfClass:[CNHomeTableViewController class]]) {
            return (CNHomeTableViewController *)next;
        }
        next = [next nextResponder];
    } while (next != nil);
    return nil;
}

@end
